var cdSelectedId;
var cdSelected;

function addCDDialog() {
	createCDDialog("Insert",function() {
		var typeCombo = dijit.byId("typeCombo");
		var store = typeCombo.store;
		typeCombo.set("value", store.getIdentity(store.objectStore.data[0]));
			
		var producerCombo = dijit.byId("producerCombo");
		var store = producerCombo.store;
		producerCombo.set("value", store.getIdentity(store.objectStore.data[0]));
		
	});
};

function submitDialogCD() {
	var dlg = dijit.byId("CDDialog");
	
	var nameCD = dijit.byId("nameCD").value;
	var producerName = dijit.byId("producerCombo").displayedValue;
	var type = dijit.byId("typeCombo").displayedValue;
	var dateField = dijit.byId("acquisitionDate");
	var data = dateField.displayedValue;
	
	if (dlg.title == "Insert") {
		submitAddCD(nameCD, producerName, type, data);
	} else {
		submitEditCD(nameCD, producerName, type, data);
	}
}

function submitAddCD(nameCD, producerName, type, data) {
	dojo.xhrGet({
		url : "/mari/retriveData",
		content : {
			'operation' : 'insertCD',
			'name' : nameCD,
			'producer' : producerName,
			'type' : type,
			'data' : data
		},
		load : function(response) {
			if (response.indexOf("Error") != 0) {
				dijit.byId("CDDialog").hide();
				// add cd in store
				var result = parseJSON(response);
				gridCDS.store.objectStore.data.push(result);
				gridCDS._refresh();
			}
		},
		handleAs : "text"
	});
}

function submitEditCD(nameCD, producerName, type, data) {
	dojo.xhrGet({
		url : "/mari/retriveData",
		content : {
			'operation' : 'editCD',
			'idcd' : cdSelectedId,
			'name' : nameCD,
			'producer' : producerName,
			'type' : type,
			'data' : data
		},
		load : function(response) {
			if (response.indexOf("Error") != 0) {
				dijit.byId("CDDialog").hide();
				// add cd in store
				var result = parseJSON(response);
				var index = gridCDS.getItemIndex(cdSelected);
				gridCDS.store.objectStore.data[index] = result;
				gridCDS._refresh();
			}
		},
		handleAs : "text"
	});
}

function saveProducer(producerName) {
	var producer = {id:"-1", name:producerName};
	producerStore.objectStore.data.push(producer);
	dijit.byId("producerCombo").set('item', producer);
}

function saveType(type) {
	var typeObj = {id:"-1", type:type};
	typeStore.objectStore.data.push(typeObj);
	dijit.byId("typeCombo").set('item', typeObj);
}

function removeCD() {
	var selectedItems = gridCDS.selection.getSelected();
    if(selectedItems.length){
		confirmDialog("Are you sure you want to delete the selected CDs?", function() {
			var selectedItemsIds="";
			dojo.forEach(selectedItems, function(selectedItem){
                if(selectedItem != null){
                	selectedItemsIds += selectedItem.cdId +",";
                }
                var id = gridCDS.getItemIndex(selectedItem);
                gridCDS.store.objectStore.data.splice(id,1);
            });
			dojo.xhrGet({ 
			   url: "/mari/retriveData", 
			   content: {'operation':'deleteCD', 'ids': selectedItemsIds}  
			});
			gridCDS._refresh();
        });
    }
};

function editCD(){
	var selectedItems = gridCDS.selection.getSelected();
    if(selectedItems.length != 1){
    	createWarningDialog("Select only one row");
		return;
    }
    
    cdSelected = selectedItems[0];
    cdSelectedId = selectedItems[0].cdId;
    
    createCDDialog("Edit", function() {
    	var nameText = dijit.byId("nameCD");
    	nameText.set("value", selectedItems[0].name);
    	var typeCombo = dijit.byId("typeCombo");
    	var store = typeCombo.store;
   		typeCombo.set("value", store.getIdentity(selectedItems[0].cdType));
    			
   		var producerCombo = dijit.byId("producerCombo");
   		var store = producerCombo.store;
   		producerCombo.set("value", store.getIdentity(selectedItems[0].producer));
    	
    	var dateTextBox = dijit.byId("acquisitionDate");
    	var dateValue = new Date(selectedItems[0].acquisitionDate);
    	dateTextBox.set("value", dateValue);
    });

    
};